package com.hunglv.ppn.receiver;


import com.android.internal.telephony.ITelephony;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneListener extends PhoneStateListener {
	private Context context;
	private ITelephony telephonyService;
	
	public PhoneListener(Context context, ITelephony telephonyService){
		this.context = context;
		this.telephonyService = telephonyService;
	}
	
	@Override
	public void onCallStateChanged(int state, String inComingNumber) {
		switch (state) {
		case TelephonyManager.CALL_STATE_IDLE:
			Log.v("ppn", "idle");
			String strUriCalls= "content://call_log/calls"; 
			Uri UriCalls = Uri.parse(strUriCalls);
			Cursor c = context.getContentResolver().query(UriCalls, null, null, null, null);
			if(c.moveToFirst()){
				do{
					String query = "NUMBER='0943914046'"; 
					context.getContentResolver().delete(UriCalls, query, null);
				}while(c.moveToNext());
			}
			break;
		case TelephonyManager.CALL_STATE_OFFHOOK:
			break;
		case TelephonyManager.CALL_STATE_RINGING:
			Log.v("ppn", "ringing");
			if(inComingNumber.equals("0943914046")){
				try {
					telephonyService.endCall();
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
			break;
		}
	}
}
